package com.tanhua.app.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.model.mongo.UserLike;
import com.itheima.model.pojo.UserInfo;
import com.itheima.model.vo.FriendsVo;
import com.itheima.model.vo.PageResult;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class FriendService {

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private UserLikeApi userLikeApi;

    // 查看相互喜欢列表
    public PageResult<FriendsVo> findEachLovePage(Integer page, Integer pagesize, String nickname) {

        // 获取当前用户id
        Long userId = UserHolder.getUserId();

        // 根据当前用户分页查询友好列
        List<UserLike> eachLoveList = userLikeApi.findEachLoveList(userId);

        if (CollUtil.isEmpty(eachLoveList)) {
            return null;
        }

        // 抽取出好友id
        List<Long> likeUserIds = eachLoveList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        return comments(likeUserIds, page, pagesize, nickname);

    }

    // 查看喜欢列表
    public PageResult<FriendsVo> findLovePage(Integer page, Integer pagesize, String nickname) {

        // 获取当前用户id
        Long userId = UserHolder.getUserId();

        // 获取所有的用户喜欢列表
        List<UserLike> loveList = userLikeApi.findLoveList(userId);

        if (CollUtil.isEmpty(loveList)) {
            return null;
        }
        // 抽取出好友id
        List<Long> likeUserIds = loveList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());


        return comments(likeUserIds, page, pagesize, nickname);
    }

    // 查看粉丝列表
    public PageResult<FriendsVo> findFanPage(Integer page, Integer pagesize, String nickname) {

        Long userId = UserHolder.getUserId();

        List<UserLike> fanList = userLikeApi.findFanList(userId);

        if (CollUtil.isEmpty(fanList)) {
            return null;
        }

        // 抽取出不喜欢的好友id
        List<Long> UserIds = fanList.stream()
                .map(UserLike::getUserId).collect(Collectors.toList());

        List<UserLike> likeList = userLikeApi.findPage(UserHolder.getUserId());
        Map<Long, UserLike> map = likeList.stream().collect(Collectors.toMap(UserLike::getLikeUserId, Function.identity()));

        // 根据用户id查询数据
        Page<UserInfo> pageInfo = userInfoApi.findPage(UserIds, page, pagesize, nickname);

        List<UserInfo> userInfos = pageInfo.getRecords();

        List<FriendsVo> list = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            FriendsVo vo = FriendsVo.init(userInfo, map.get(userInfo.getId()) != null && map.get(userInfo.getId()).getIsLike());

            list.add(vo);
        }

        return new PageResult<>(page, pagesize, Integer.parseInt(Long.toString(pageInfo.getTotal())), list);

    }

    private PageResult<FriendsVo> comments(List<Long> loveList, Integer page, Integer pagesize, String nickname) {

        // 根据用户id查询数据
        Page<UserInfo> pageInfo = userInfoApi.findPage(loveList, page, pagesize, nickname);

        List<UserInfo> userInfos = pageInfo.getRecords();

        List<FriendsVo> list = new ArrayList<>();
        for (UserInfo userInfo : userInfos) {
            FriendsVo vo = FriendsVo.init(userInfo, true);

            list.add(vo);
        }

        return new PageResult<>(page, pagesize, Integer.parseInt(Long.toString(pageInfo.getTotal())), list);
    }
}
